<style lang="less">
  #div_order_distinguish_config {
    .div-com-sy {
      width: 100%;
    }
    .dialog-footer {
      height: 48px;
      border-bottom: 1px solid;
    }
  }
</style>
<template>
  <div id="div_order_distinguish_config">
    <div>
      <div class="div-com-sy f-mt10">零担分单规则</div>
      <el-form :model="LDForm" label-width="110px" ref="LDForm">
        <el-row class="f-mt10 f-mb10">
          <el-col :span="12">
            <el-form-item label="分单规则" prop="distinguishNodeId"
                          :rules=" {required: true,type:'number',message: '请选择分单规则', trigger: 'change'}">
              <el-radio-group v-model="LDForm.distinguishNodeId">
                <el-radio :label="101001111">按订单起运地</el-radio>
                <el-radio :label="101001112">按订单目的地</el-radio>
                <el-radio :label="101001113">按关联合伙人</el-radio>
                <el-radio :label="101001114">按订单起运地 & 目的地或起运地</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="f-mt10 f-mb10">
          <el-col :span="12">
            <el-form-item label="默认组织"
                          :rules="{required: true, type: 'array', message: '请选择默认组织', trigger: 'change'}">
              <el-cascader
                placeholder="请选择默认组织"
                style="width: 80%"
                :options="$store.state.initOrganizations"
                :props="{label: 'name',value: 'id'}"
                change-on-select
                v-model="LDForm.defaultOrganizations"></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveByTypeClick('LD')">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { saveOrderDistinguishConfig, get } from '../../api/order/orderDistinguishConfig.js';
import { getSelectObect, isEmpty } from '../../lib/util.js';

export default {
  data () {
    return {
      LDForm: {
        id: '',
        distinguishNodeId: '',
        distinguishNodeName: '',
        defaultOrganizations: []
      },
      distinguishNodes: [
        {key: 101001111, vu: '按订单起运地'},
        {key: 101001112, vu: '按订单目的地'},
        {key: 101001113, vu: '按关联合伙人'},
        {key: 101001114, vu: '按关单起运地&目的地或起运地'}
      ]
    };
  },
  methods: {
    saveClick (refsForm, val) {
      if (isEmpty(val)) {
        return this.$message.error('提交的表单不能为空');
      }
      refsForm.validate((valid) => {
        if (valid) {
          let form = Object.assign({}, val);
          form.distinguishNodeName = getSelectObect(form.distinguishNodeId, 'key', this.distinguishNodes).vu;
          form.defaultOrganizations = form.defaultOrganizations.join(',');
          saveOrderDistinguishConfig(form).then((res) => {
            if (res.data.successFlg) {
              this.getorderDistinguishConfig();
              this.$message.success('设置成功');
            } else {
              this.$message.error('设置失败');
            }
          });
        }
      });
    },
    saveByTypeClick (type) {
      let form = null;
      let refsForm = this.$refs['LDForm'];
      if (type === 'LD') {
        refsForm = this.$refs['LDForm'];
        form = Object.assign(this.LDForm, {
          distinguishTypeId: '101001115',
          distinguishTypeName: '零担'
        });
      }
      this.saveClick(refsForm, form);
    },
    getorderDistinguishConfig () {
      get('').then((res) => {
        let list = res.data.detailModelList;
        for (let l in list) {
          if (list[l].distinguishTypeId === 101001115) {
            this.LDForm = list[l];
            this.LDForm.defaultOrganizations = list[l].defaultOrganizations.split(',');
          }
        }
      });
    }
  },
  mounted () {
    this.$store.commit('initOrganizations');
    this.getorderDistinguishConfig();
  }
};
</script>
